from rest_framework import serializers
from rest_framework.generics import CreateAPIView, RetrieveUpdateAPIView, ListAPIView, RetrieveAPIView, UpdateAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet
import re
from user.models import User
from user.serializer import *


# 关注


# 标签
class LabelsView(APIView):

    def put(self,request):
        instance = request.user
        serializer = LabelDSerializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data)

# 修改密码
class ModifyPwsView(APIView):
    def put(self,request):
        new_password = request.data.get('password')
        if not new_password:
            return Response({
                "errno":4004,
                "errmsg":'缺少参数'
            })

        if not re.match(r'^[A-Za-z0-9]{6,19}$',new_password):
            return Response({
                "errno": 4004,
                "errmsg": '缺少参数'
            })
        user = request.user
        user.set_password(new_password)
        user.save()

        return Response({
            'errno':0,
            'errmsg':'修改成功'
        })

# 个人中心详情
class DetailView(APIView):

    def get(self, request):
        # 获取对象
        user = request.user
        serializers = UserDetailSerializer(user)
        return Response(serializers.data)

    def put(self,request):
        instance = request.user
        serializer = UserDetailSerializer(instance, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data)

# 注册
class RegisterView(CreateAPIView):
    queryset = User.objects.all()
    serializer_class = UserRegisterSerializer

# # 登录
# class LoginView(APIView):
#     def post(self,request):
#         # 實例化對象
#         serializer = UserLoginSerializer(data=request.data)
#
#         #啓動檢驗
#         serializer.is_valid(raise_exception=True)
#
#         # 提取參數
#         user= serializer.validated_data.get('user')
#         token = serializer.validated_data['token']
#
#         # 構建響應
#         return Response({
#             'mobile':user.mobile,
#             'id':user.id,
#             'token':token,
#             'avatar':user.avatar,
#             'username':user.username
#         })